$(function () {
    // html font-size 的设置， 当 device 的宽度 > 1438px 时，font-size 依旧是 100px
    var deviceWidth = document.documentElement.clientWidth;

    if (deviceWidth > 1438) deviceWidth = 1438;
    document.documentElement.style.fontSize = deviceWidth / 13.66 + 'px';

    $(".main-nav .first-nav").on('mouseleave', function () {

        $(this).find('.inSubNav').slideUp(500);

    }).on('mouseenter', function () {

        $('nav .inSubNav').finish();//结束当前正在进行的动画

        var $level2_nav = $(this).find('.inSubNav').slideDown(500);

        var subwidth = getSubWidth($level2_nav);

        var padleft = $(this).position().left + $(this).outerWidth() / 2 - subwidth / 2;

        var min_padleft = $('.innav').offset().left;

        var max_padleft = $('.innav').offset().left + $('.innav').width() - subwidth;

        if (padleft < min_padleft) padleft = min_padleft;

        if (padleft > max_padleft) padleft = max_padleft;

        $level2_nav.css('padding-left', padleft + 'px');

        function getSubWidth($level2_nav) {
            var innerwidth = 0;
            $level2_nav.find('li').each(function (index, element) {
                innerwidth += $(element).outerWidth();
            });
            return innerwidth;
        }
    });


})




